<?php

class IndexController extends MemberBaseController
{
	/**
	 * @var string the default layout for the views. Defaults to '//layouts/column2', meaning
	 * using two-column layout. See 'protected/views/layouts/column2.php'.
	 */
	public $layout='//layouts/member';
	public $breadcrumbs=array();
	public $reporttype = array(
			'5'=>'出售禁售品',
			'6'=>'出售假货',
			'7'=>'举报知识产权侵权',
			'8'=>'图片实拍保护维权');
	public $refundReason = array(
			'0'=>'请选择退款原因',
			'1'=>'我不想买了',
			'2'=>'暂时缺货',
			'3'=>'协商一致退款',
			'4'=>'未收到货',
			'5'=>'质量问题',
			'6'=>'商品不满意',
			'7'=>'其他');
	public $aftersaleReason = array(
			'0'=>'请选择退款原因',
			'3'=>'协商一致退款',
			'4'=>'未收到货',
			'5'=>'质量问题',
			'6'=>'商品不满意',
			'7'=>'其他');
	public $refundType = array(
			'0'=>'请选择退款类型',
			'1'=>'退款至账户余额',
			'2'=>'退款至银行卡',
			'3'=>'退款至支付的账户或银行卡');
	public $complainType = array(
			'0'=>'请选择投诉类型',
			'1'=>'违背承诺',
			'2'=>'描述不符',
			'3'=>'未收到货',
			'4'=>'售后保障服务',
			'9'=>'其他');
	/**
	 * @return array action filters
	 */
	public function filters()
	{
		return array(
			//'accessControl', // perform access control for CRUD operations
		);
	}
	
	//验证码
	public function actions()
	{
		return array(
				// captcha action renders the CAPTCHA image displayed on the contact page
				'captcha'=>array(
						'class'=>'CCaptchaAction',
						'backColor'=>0xFFFFFF,
						'minLength'=>4,  //最短为4位
						'maxLength'=>4,   //是长为4位
						'height'=>'35',
						'width'=>'100',
						'padding'=>'0',
				),
				// page action renders "static" pages stored under 'protected/views/site/pages'
				// They can be accessed via: index.php?r=site/page&view=FileName
				'page'=>array(
						'class'=>'CViewAction',
				),
		);
	}

	

	/**
	 * 会员中心首页.
	 */
	public function actionIndex()
	{
		$orderAmounts  = Order::model()->getOrderAmountOfStatus(Yii::app()->user->userid,1);
		$ticketAmounts = Ticket::model()->amountOfStatus(Yii::app()->user->userid);
		$history = MemHistory::model()->readBrowsingHistory();
		//收藏产品
		$criteria=new CDbCriteria;
		$criteria->compare('t.zm_id',Yii::app()->user->getId());
		$criteria->compare('zmf_type',1);
		$criteria->limit = 5;
		$favProduct = MemFav::model()->findAll($criteria);
		$tuan = ProTuan::model()->findAll(array(
				'condition'=>'zpt_close=1 AND zpt_status=2 AND zpt_type=1',
				'order'=>'zpt_listorder DESC',
				'limit'=> 5,
			));
		$buytime = ProTuan::model()->findAll(array(
				'condition'=>'zpt_close=1 AND zpt_status=2 AND zpt_type=2',
				'order'=>'zpt_listorder DESC',
				'limit'=> 5,
			));
		//print_r($history);exit;
		$this->render('index',array(
				'orderAmounts'=>$orderAmounts,
				'ticketAmounts'=>$ticketAmounts,
				'favProduct'=>$favProduct,
				'history'=>$history,
				'tuan'=>$tuan,
				'buytime'=>$buytime,
				));
	}

	/**
	 * 已买到的宝贝
	 */
	public function actionOrders()
	{
		$model=new Order();
		//搜索参数
		$status   = Yii::app()->request->getParam('status',0);
		$time     = Yii::app()->request->getParam('time',0);
		$keywords = Yii::app()->request->getParam('keywords',0);
		$userid   = Yii::app()->user->userid;
		$list     = $model->getMyOrdersList( $userid , $status , $time , $keywords);
		//$this->render('orders');
		//各状态的订单数
		Setting::model()->destructCache();
		$statusAmount = $model->getOrderAmountOfStatus(Yii::app()->user->userid,1);
		$this->render('orders',array(
				'model'=>$model,
				'data'=>$list['data'],
				'pager'=>$list['pager'],
				'statusAmount'=>$statusAmount
				));
	}

	/**
	 * 订单详情
	 */
	public function actionViewOrder()
	{
		$id = Yii::app()->request->getParam('orderid',0);
		$model = Order::model();
		$order = $model->getOrderInfo($id,Yii::app()->user->userid);
		//print_r($order);
		$this->render('vieworder',array('order'=>$order));
	}

	/**
	 * 提醒卖家发货
	 */
	public function actionRemindSeller($orderid)
	{
		$id = Yii::app()->request->getParam('orderid',0);
		$record = Order::model()->findByPk($id,'zm_id='.Yii::app()->user->userid);
		//已付款未发货
		if($record && $record->zo_pay_status==2 && $record->zo_exp_status==1){
			$product = OrdProduct::model()->getOrderProduct($orderid);
			OrdMsg::model()->setOrdMsg( $product[0]['zm_id'], $orderid, 3);
			$this->render('success',array( 
            'message'=>'已经提醒卖家发货！是否返回原来页面？', 
            'links'=>array( 
                    array('查看所有订单','/member/index/orders/'), 
                    array('返回会员中心','/member/index'), 
                    ), 
			)); 
			exit;			
		}
	}

	/**
	 * 查看物流
	 */
	public function actionOrderLogistics()
	{
		
	}

	/**
	 * 修改订单留言
	 */
	public function actionUpdateNote()
	{
		$id = Yii::app()->request->getParam('orderid',0);
		$record = Order::model()->findByPk($id,'zm_id='.Yii::app()->user->userid);
		if($record && $record->zo_status==1 && $record->zo_exp_status==1){
			$record->zo_postscript = Yii::app()->request->getParam('note');
			$record->update();
			$this->render('success',array( 
            'message'=>'订单留言修改成功！', 
            'links'=>array( 
                    array('查看订单','/member/index/orders/vieworder/orderid/'.$id), 
                    array('查看所有订单','/member/index/orders/'), 
                    ), 
			));
			exit; 			
		}else{
			$this->render('error',array( 
            'message'=>'对不起，订单发货后留言不可修改！', 
            'links'=>array( 
                    array('查看订单','/member/index/orders/vieworder/orderid/'.$id), 
                    array('查看所有订单','/member/index/orders/'), 
                    ), 
			));
			exit; 			
		}
	}

	/**
	 * 取消订单
	 */
	public function actionCancelOrder()
	{
		$id = Yii::app()->request->getParam('orderid',0);
		$record = Order::model()->findByAttributes(array('zo_id'=>$id,'zm_id'=>Yii::app()->user->userid));

		if($record && $record->zo_pay_status==1 || $record->zo_pay_status==5){
			$record->zo_status = 2;
			$record->update();
			$this->render('success',array( 
            'message'=>'订单取消成功！', 
            'links'=>array(                     
                    array('查看所有订单','/member/index/orders/'), 
                    ), 
			));
		}
		else
		{
			echo "a";
		}
	}

	/**
	 * 删除订单（逻辑删除）
	 */
	public function actionDeleteOrder()
	{
		$id = Yii::app()->request->getParam('orderid',0);
		$record = Order::model()->findByPk($id,'zm_id='.Yii::app()->user->userid);
		if($record && ($record->zo_status==2 || $record->zo_status==3)){
			$record->zo_del = $record->zo_del == 1 || $record->zo_del == 2 ? 2 : 4;
			$record->update();
			$this->render('success',array( 
            'message'=>'订单删除成功', 
            'links'=>array(                     
                    array('查看所有订单','/member/index/orders/'), 
                    ), 
			));			
		}
	}

	/**
	 * 评价/修改 订单评价
	 */
	public function actionReviewOrder()
	{
		$model = new OrdComment();
		if($pk = Yii::app()->request->getParam('commentid',0)){
			//$record = $model->findByPk($pk,'zm_id=:mid',array(':mid'=>Yii::app()->user->userid));
			$record = $model->findByPk($pk);
			$model = $record ? $record : $model;
		}

		$orderid        = !empty($_GET['orderid'])?$_GET['orderid']:$record->zo_id; 
		//取订单		
		$order          = Order::model()->find('zo_id='.$orderid);
		//取订单产品           
        $product        = $order->orderProduct;
        if($_POST){
        	$comment    = !empty($_POST['comment'])?$_POST['comment']:''; 
        	$rate       = !empty($_POST['rate'])?$_POST['rate']:''; 
        	//print_r($_POST);exit;
        	$ordercomment =array();
        	//将评论结果写入评论表
        	foreach ($product as $key => $value) {
        		$model = OrdComment::model()->findByAttributes(array('zo_id'=>$orderid,'zop_id'=>$value->zop_id));
        		if(empty($model))
        			$model = new OrdComment();
        		$ordercomment['zo_id'] = $orderid;
        		$ordercomment['zop_id'] = $value->zop_id;
        		$ordercomment['zp_id'] = $value->zp_id;
        		$ordercomment['zm_id'] = $value->zm_id;
        		$ordercomment['zoc_spe'] = $value->zop_spe;
        		$ordercomment['zoc_content'] = $comment[$value->zop_id]['zoc_content'];
        		$ordercomment['zoc_product'] = $rate['zoc_product'];
        		$ordercomment['zoc_service'] = $rate['zoc_service'];
        		$ordercomment['zoc_express'] = $rate['zoc_express'];
        		$ordercomment['zoc_sum'] = $comment[$value->zop_id]['zoc_sum'];
        		$ordercomment['zoc_type'] = 2;
        		$ordercomment['zoc_status'] = 2;
        		$ordercomment['zoc_status'] = 2;
        		$ordercomment['inputtime'] = time(); 
        		//print_r($ordercomment);exit;
        		$model->attributes = $ordercomment;
        		if($model->save()){
        			$ok = true ;
        		}			

        	}
        	//print_r($ok);exit;
        	if( $ok== true ){
        		//写入订单评论状态
        		$orders =array();        		
        		$orders['zo_id'] = $orderid;
        		$orders['zo_comment'] = 2;
        		$orders['zo_voer_time'] = time();
        		if(isset($orders))
				{
					$order->attributes = $orders;
        			$order->save();
        		}
	        	$this->render('revieworder',array(
							'okcomment'=> 1,
				));
			}
        }

		$this->render('revieworder',array(
				'product' => $product,
				'order' => $order,
				'model'=>$model,
				));
	}

	/**
	 * 来自他人的评价（我是买家）
	 */
	public function actionCommentToMe()
	{
		$level = Yii::app()->request->getParam('level', 0);
		$conments = OrdComment::model()->getComments(Yii::app()->user->userid, 1, 1, $level);
		$credit = OrdComment::model()->getAmountOfLevels(Yii::app()->user->userid, 1);
		$this->render('commenttome',array(
				'credit'=>$credit,
				'comments'=>$conments['comments'],
				'pager'=>$conments['pager']
				));
	}

	/**
	 * 给他人的评价（我是买家）
	 */
	public function actionCommentFromMe()
	{
		$level = Yii::app()->request->getParam('level', 0);
		$conments = OrdComment::model()->getComments(Yii::app()->user->userid, 1, 2, $level);
		$credit = OrdComment::model()->getAmountOfLevels(Yii::app()->user->userid, 1);
		$this->render('commentfromme',array(
				'credit'=>$credit,
				'comments'=>$conments['comments'],
				'pager'=>$conments['pager']
				));
	}

	/**
	 * 收货地址
	 */
	public function actionAddresses()
	{
		$model = MemAddres::model();
		$this->render('addresses',array('model'=>$model));
	}

	/**
	 * 增加或修改收货地址
	 */
	public function actionAddAddress()
	{
		$model = MemAddres::model();
		if(isset($_POST['MemAddres']))
		{
			$model = new MemAddres();
			if($zma_id = $_POST['MemAddres']['zma_id']){
				//是否该会员的地址
				$model = $model->findByPk($zma_id,'zm_id=:mid',array(':mid'=>Yii::app()->user->userid));
				if(!$model){
					$this->render('error',array( 
		            'message'=>'对不起，请求超时或非法提交', 
		            'links'=>array(                     
		                    array('管理收货地址','/member/index/addresses'), 
		                    ), 
					));
					exit;
				}
			}else{
				//数量限制
				$count = $model->count('zm_id=:mid',array(':mid'=>Yii::app()->user->userid));
				if($count>=10){
					$this->render('error',array( 
		            'message'=>'对不起，收货地址最多设置10条', 
		            'links'=>array(                     
		                    array('管理收货地址','/member/index/addresses'), 
		                    ), 
					));					
					exit;
				}
			}
			$model->attributes = $_POST['MemAddres'];
			$model->zm_id = Yii::app()->user->userid;
			if($model->save()){
					header301('/member/index/addresses');
					exit;					
			}
		}elseif($pk = Yii::app()->request->getParam('aid',0)){
			$record = $model->findByPk($pk,'zm_id=:mid',array(':mid'=>Yii::app()->user->userid));
			$model = $record ? $record : $model;
		}
		$this->render('addaddress',array('model'=>$model));
	}

	/**
	 * 更改默认收货地址
	 */
	public function actionDefaultAddress()
	{
		
	}

	/**
	 * 删除收货地址
	 */
	public function actionDelAddress()
	{
		$id = Yii::app()->request->getParam('aid',0);
		MemAddres::model()->deleteByPk($id,'zm_id=:mid',array(':mid'=>Yii::app()->user->userid));
		header301('/member/index/addresses');
		//echo '<script>if(confirm("删除成功。")){self.location.href="'.Yii::app()->createUrl('member/index/addresses').'";}</script>';
		Yii::app()->end();
	}

	/**
	 * 我的收藏—产品
	 */
	public function actionFavorites()
	{
		$model = new MemFav('search');
		$result = $model->getFoverProduct();
		//print_r($fav);exit;
		$this->render('favoriteproducts',
				array(
						'fav'=>$result['data'],
						'pager'=>$result['pager'])
				);
	}

	/**
	 * 我的收藏—店铺
	 */
	public function actionFavoriteShops()
	{
		$model = new MemFav('search');
		$result = $model->getFoverShop();
		$this->render('favoriteshops',
				array(
						'fav'=>$result['data'],
						'pager'=>$result['pager'])
				);
	}

	/**
	 * 删除收藏
	 */
	public function actionDeleteFavorite()
	{
		$ids = Yii::app()->request->getParam('ids',0);
		$idbox = array(0);
		if(is_array($ids) && count($ids)){
			foreach($ids as $key=>$id){
				(int)$id && $idbox[] = (int)$id;
			}
		}elseif($ids){
			(int)$ids && $idbox[] = (int)$ids;
		}else{
			$message = '删除失败，未选中任何收藏。';
		}
		if(count($idbox)>1 && MemFav::model()->deleteFav($idbox, Yii::app()->user->userid)){
			$message = '删除成功。';
		}elseif(count($idbox)>1){
			$message = '删除失败。';
		}
		$this->render('success',array( 
		            'message'=>$message, 
		            'links'=>array(                     
		                    array('查看我的收藏','/member/index/favorites'), 
		                    ), 
					));	
		
	}

	/**
	 * 退款管理
	 */
	public function actionRefund()
	{
		$model  = Refund::model();
		$this->render('refund',array('model'=>$model));
	}

	/**
	 * 查看退款详情
	 */
	public function actionViewRefund()
	{
		if($refundid = Yii::app()->request->getParam('refundid',0)){
			//判断是否卖家未处理的退款
			$model =  Refund::model()->findByPk($refundid);
			if(!$model || $model->zr_kind!=1){
				$this->render('error',array( 
		            'message'=>'没有对应的退款', 
		            'links'=>array(                     
		                    array('查看我的退款','/member/index/refund'), 
		                    ), 
					));
				exit;	
			}elseif($model->zr_cmp_time && !$model->zr_return){
				//商家已处理，跳转至第三步
				$this->redirect(array('/member/index/refundthree','refundid'=>$refundid));
			}else{
				//商家未处理，跳转至第二步
				$this->redirect(array('/member/index/refundtwo','refundid'=>$refundid));
			}
		}
	}

    /*
	 *申请退款第一步,判断是否可退款，计算金额，渲染页面
	 */
	 public function actionRefundOne(){	 
	 	if($id = Yii::app()->request->getParam('ordproductid',0)){
	 		$model  = Refund::model();
	 		$model->zr_id = Tool::get_order_id();
	 		$model->zr_amount = OrdProduct::model()->calculateForRefund($id, Yii::app()->user->userid, 1, 1);
	 		$model->zop_id = $id;
			$ordproduct = OrdProduct::model()->findByPk($model->zop_id);
	 		$product = Product::model()->findByPk($ordproduct->zp_id);
	 		$order = Order::model()->findByPk($ordproduct->zo_id);
	 		$discount = OrdDiscount::model()->findAllByAttributes(array('zop_id'=>$model->zop_id));
			$this->render('refundone',array(
					'model'=>$model,
					'product'=>$product,
					'ordproduct'=>$ordproduct,
					'order'=>$order,
					'discount'=>$discount)
			);
	 	}else{
	 		$this->render('error',array( 
		            'message'=>'参数错误',
					));
				exit;	
        }
	 }
	 /*
	 *申请退款第二步
	 */
	 public function actionRefundTwo(){
	 	$model =  new Refund();
	 	if(isset($_POST['Refund']))
		{
			//print_r($_POST);exit;
			$model->attributes=$_POST['Refund'];
			$amount = OrdProduct::model()->calculateForRefund($model->zop_id, Yii::app()->user->userid, 1, 1);
			if($model->zr_amount > $amount){
				$this->render('error',array( 
		            'message'=>'申请退款金额不可大于该商品退款限额，请输入正确值。',
					));
				exit;				
			}
			$ordproduct = OrdProduct::model()->findByPk($model->zop_id);
			$order = Order::model()->findByPk($ordproduct->zo_id);
			$product = Product::model()->findByPk($ordproduct->zp_id);
			$model->zo_id    = $order->zo_id;
			$model->zm_id    = $order->zm_id;
			$model->zp_id    = $ordproduct->zp_id;
			$model->zr_title = $ordproduct->zop_title;
			$model->zr_pay_status = 1;
			$model->zr_order_status = $model->createOrderStatus($order);
			$model->zr_kind  = 1;
			$model->inputtime = time();
			//图片
			$images = isset($_POST['images']) ? array_filter($_POST['images']) : array();
			$model->zr_user_pic = array_slice($images, 0, 3);
			//增加相关数据，如订单号，商品号，等等
			if($model->save()){
				//更新订单状态，订单产品状态
				$ordproduct->zop_status=2;
				$ordproduct->save();
				$order->zo_pay_status=4;
				if ($order->save()) {
					//exit('申请成功，请等待卖家处理');
					$this->render('success',array( 
			            'message'=>'申请成功，请等待卖家处理',
			             'links'=>array( 
			                    array('已买到的宝贝','/member/index/orders'), 
			                    array('返回首页','/'), 
		                    ),  
						));
					exit;
				}
				//重定向到本页，传入refundid
				$this->redirect($this->route,array('refundid'=>Yii::app()->db->lastInsertID));
			}
		}elseif($refundid = Yii::app()->request->getParam('refundid',0)){
			//判断是否卖家未处理的退款
			$model =  Refund::model()->findByPk($refundid);
			if(!$model || $model->zr_kind!=1){
				$this->render('error',array( 
		            'message'=>'没有对应的退款',
					));
				exit;				
			}elseif($model->zr_cmp_time){
				//商家已处理，跳转至第三步
			}
			$ordproduct = OrdProduct::model()->findByPk($model->zop_id);
			$product    = Product::model()->findByPk($ordproduct->zp_id);
			$discount   = OrdDiscount::model()->findAllByAttributes(array('zop_id'=>$model->zop_id));
			$this->render('refundtwo',array(
					'model'=>$model,
					'product'=>$product,
					'ordproduct'=>$ordproduct,
					'discount'=>$discount)
			);
			Yii::app()->end();
		}else{
			$this->render('error',array( 
		            'message'=>'参数错误',
					));
				exit;				
		}
		$this->render('refundtwo',array('model'=>$model));
	 }
    /*
     * 申请退款第三步
     * 
     */
	 public function actionRefundThree(){
	 	if($refundid = Yii::app()->request->getParam('refundid',0)){
			//判断是否卖家未处理的退款
			$model =  Refund::model()->findByPk($refundid);
			if(!$model){
				$this->render('error',array( 
		            'message'=>'没有对应的退款',
					));
				exit;				
			}elseif(!$model->zr_cmp_time){
				//商家未处理，跳转至第二步
			}
			$ordproduct = OrdProduct::model()->findByPk($model->zop_id);
			$product = Product::model()->findByPk($ordproduct->zp_id);
			$discount = OrdDiscount::model()->findAllByAttributes(array('zop_id'=>$model->zop_id));
			$this->render('refundthree',array(
					'model'=>$model,
					'product'=>$product,
					'ordproduct'=>$ordproduct,
					'discount'=>$discount)
			);
		}else{
			$this->render('error',array( 
		            'message'=>'参数错误',
					));
				exit;
		}
	 }
	 
    /*
     * 取消退款
     * 
     */
	 public function actionCancelRefund(){
	 	if($refundid = Yii::app()->request->getParam('refundid',0)){
	 		$model =  Refund::model()->findByPk($refundid);
	 		$model->zr_pay_status = 5;
	 		if($model->save()){
	 			$this->render('success',array( 
		            'message'=>'取消退款成功',
					));
				exit;		
	 		}
	 	}
	 }

	/**
	 * 售后管理
	 */
	public function actionAftersale()
	{		
		$model  = Refund::model();
		$this->render('aftersale',array('model'=>$model));
	}

	/**
	 * 售后申请一
	 */
	public function actionAftersaleOne()
	{
		if($id = Yii::app()->request->getParam('ordproductid',0)){
			$model  = Refund::model();
			$model->zr_amount = OrdProduct::model()->calculateForRefund($id, Yii::app()->user->userid, 1, 1);
			$model->zop_id = $id;
			$ordproduct = OrdProduct::model()->findByPk($model->zop_id);
			$product = Product::model()->findByPk($ordproduct->zp_id);
			$discount = OrdDiscount::model()->findAllByAttributes(array('zop_id'=>$model->zop_id));
			$this->render('aftersaleone',array(
					'model'=>$model,
					'product'=>$product,
					'ordproduct'=>$ordproduct,
					'discount'=>$discount
					));
		}else{
			$this->render('error',array( 
		            'message'=>'参数错误',
					));
				exit;
		}
	}

	/**
	 * 售后申请二
	 */
	public function actionAftersaletwo()
	{
	 	$model =  new Refund();
	 	if(isset($_POST['Refund']))
		{
			$model->attributes=$_POST['Refund'];
			$amount = OrdProduct::model()->calculateForRefund($model->zop_id, Yii::app()->user->userid, 1, 1);
			if($model->zr_amount > $amount){
				$this->render('error',array( 
		            'message'=>'申请退款金额不可大于该商品退款限额，请输入正确值。',
					));
				exit;
			}
			$ordproduct = OrdProduct::model()->findByPk($model->zop_id);
			$order = Order::model()->findByPk($ordproduct->zo_id);
			$product = Product::model()->findByPk($ordproduct->zp_id);
			$model->zo_id = $order->zo_id;
			$model->zm_id = $order->zm_id;
			$model->zp_id = $ordproduct->zp_id;
			$model->zr_title = $ordproduct->zop_title;
			$model->zr_pay_status = 1;
			$model->zr_kind = 2;
			$model->inputtime = time();
			//图片
			$images = isset($_POST['images']) ? array_filter($_POST['images']) : array();
			$model->zr_user_pic = array_slice($images, 0, 3);
			//增加相关数据，如订单号，商品号，等等
			if($model->save()){
				//更新订单状态，订单产品状态
								
				exit('申请成功，请等待卖家处理');
				//重定向到本页，传入refundid
			}
		}elseif($refundid = Yii::app()->request->getParam('refundid',0)){
			//判断是否卖家未处理的退款
			$model =  Refund::model()->findByPk($refundid);
			if(!$model || $model->zr_kind!=2){
				$this->render('error',array( 
		            'message'=>'没有对应的售后信息',
					));
				exit;				
			}elseif($model->zr_cmp_time){
				//商家已处理，跳转至第三步
			}
			$ordproduct = OrdProduct::model()->findByPk($model->zop_id);
			$product = Product::model()->findByPk($ordproduct->zp_id);
			$discount = OrdDiscount::model()->findAllByAttributes(array('zop_id'=>$model->zop_id));
			$this->render('refundtwo',array(
					'model'=>$model,
					'product'=>$product,
					'ordproduct'=>$ordproduct,
					'discount'=>$discount
					));
		}else{
			$this->render('error',array( 
		            'message'=>'参数错误',
					));
				exit;
		}
	}

	/**
	 * 售后申请三
	 */
	public function actionAftersalethree()
	{
	 if($refundid = Yii::app()->request->getParam('refundid',0)){
			//判断是否卖家未处理的退款
			$model =  Refund::model()->findByPk($refundid);
			if(!$model){
				$this->render('error',array( 
		            'message'=>'没有对应的售后信息',
					));
				exit;				
			}elseif(!$model->zr_cmp_time){
				//商家未处理，跳转至第二步
			}
			$ordproduct = OrdProduct::model()->findByPk($model->zop_id);
			$product = Product::model()->findByPk($ordproduct->zp_id);
			$discount = OrdDiscount::model()->findAllByAttributes(array('zop_id'=>$model->zop_id));
			$this->render('aftersalethree',array(
					'model'=>$model,
					'product'=>$product,
					'ordproduct'=>$ordproduct,
					'discount'=>$discount
					));
		}else{
			$this->render('error',array( 
		            'message'=>'参数错误',
					));
				exit;
		}
	}

	/**
	 * 买家发货单填写
	 */
	public function actionShipment()
	{
		if($refundid = Yii::app()->request->getParam('refundid',0)){
			//判断是否卖家未处理的退款
			$model =  Refund::model()->findByPk($refundid);
			if(!$model){
				$this->render('error',array( 
		            'message'=>'没有对应的售后信息',
					));
				exit;				
			}elseif($model->zr_pay_status!=3){
				//不处于商家同意的状态，不是填写物流的步骤，提示错误
				$this->render('error',array( 
		            'message'=>'没有对应的退款或售后需要填写物流信息',
					));
				exit;				
			}
			$ordproduct = OrdProduct::model()->findByPk($model->zop_id);
			$product = Product::model()->findByPk($ordproduct->zp_id);
			$discount = OrdDiscount::model()->findAllByAttributes(array('zop_id'=>$model->zop_id));
			//退货地址
			$address = implode('，',array_filter(array(Member::model()->getFields($product->zm_id)->more['zmp_firstname'],Area::getAreaStrByFloorId($model->zar_id),$model->zr_address,$model->zr_zip,$model->zr_tel)));
			$this->render('shipment',array(
					'model'     =>$model,
					'product'   =>$product,
					'ordproduct'=>$ordproduct,
					'discount'  =>$discount,
					'address'   =>$address
					));
		}elseif($_POST['Refund']){
			$model =  Refund::model();
			$model->zr_id         = $_POST['Refund']['zr_id'];
			$model->attributes    = $_POST['Refund'];
			$model->zr_pay_status = 3;
			if($model->save()){
				//提示成功
			}else{
				echo $model->getErrorsStr();
			}		
		}else{
			$this->render('error',array( 
		            'message'=>'参数错误',
					));
				exit;
		}
	}

	/**
	 * 投诉管理
	 */
	public function actionComplain()
	{
		$model = MemReport::model();
		$this->render('complain',array('model'=>$model));
	}

	/**
	 * 投诉一
	 */
	public function actionComplainOne()
	{
		if($id = Yii::app()->request->getParam('ordproductid',0)){
				$model  = MemReport::model();
				$model->zop_id = $id;
				$ordproduct = OrdProduct::model()->findByPk($model->zop_id);
				$product = Product::model()->findByPk($ordproduct->zp_id);
				$order = Order::model()->findByPk($ordproduct->zo_id);
				$discount = OrdDiscount::model()->findAllByAttributes(array('zop_id'=>$model->zop_id));
				$this->render('complainone',array(
						'model'=>$model,
						'product'=>$product,
						'ordproduct'=>$ordproduct,
						'order'=>$order,
						'discount'=>$discount)
				);
		}else{
			$this->render('error',array( 
		            'message'=>'参数错误',
					));
				exit;
		}
	}

	/**
	 * 投诉二
	 */
	public function actionComplainTwo()
	{
		if(isset($_POST['MemReport'])){
			$zop_id = $_POST['MemReport']['zop_id'];
			$zp_id  = $_POST['MemReport']['zp_id'];
			$typeinfo = $_POST['MemReport']['zmr_type_info'];

			//限制会员一个产品一种类型只能投诉一次
			$oldreport = MemReport::model()->findByAttributes(
					array('zop_id'=>$zop_id,
						  'zmr_type'=>'1',
						  'zmr_type_info'=>$typeinfo,
						  'zm_id_from'=>Yii::app()->user->userid));
			/*这里报错 */
			if(!empty($oldreport->zmr_id)>0){
				$this->render('error',array( 
		            'message'=>'该产品已经举报过，请查看记录',
					));
				exit;				
			}
			$ordproduct = OrdProduct::model()->findByPk($zop_id);
			$product = Product::model()->findByPk($ordproduct->zp_id);
			$discount = OrdDiscount::model()->findAllByAttributes(array('zop_id'=>$zop_id));	
				

			$model = new MemReport();
			$model->attributes  =$_POST['MemReport'];
			//print_r($model->attributes);EXIT;
			$model->zmr_id      = substr(time(),-8).rand(111,999);
			$model->zmr_type    =1;
			$model->inputtime   =time();
			$model->zo_id   = $ordproduct->zo_id;
			//图片
			$images = array_filter($_POST['MemReport']['zmr_mem_pic']);
			$model->zmr_mem_pic = array_slice($images, 0, 3);
			$model->save();
			$model->zmr_mem_pic = unserialize($model->zmr_mem_pic);
			$this->render('complaintwo',array(
					'model'=>$model,
					'productinfo'=>$product,
					'product'=>$product,
					'ordproduct'=>$ordproduct,
					'discount'=>$discount
			));
		}
		//订单列表直接查看订单结果
		elseif($id = Yii::app()->request->getParam('memreportid',0))
		{
			$memreport = MemReport::model()->findByAttributes(array('zmr_id'=>$id));
			$ordproduct = OrdProduct::model()->findByPk($memreport->zop_id);
			$product = Product::model()->findByPk($ordproduct->zp_id);
			$discount = OrdDiscount::model()->findAllByAttributes(array('zop_id'=>$memreport->zop_id));
			
			$this->render('complaintwo',array(
					'model'=>$memreport,
					'productinfo'=>$product,
					'product'=>$product,
					'ordproduct'=>$ordproduct,
					'discount'=>$discount
			));
		}
	}

	/**
	 * 投诉三
	 */
	public function actionComplainThree()
	{
		if($id = Yii::app()->request->getParam('memreportid',0))
		{
			$memreport = MemReport::model()->findByAttributes(array('zmr_id'=>$id));
			$ordproduct = OrdProduct::model()->findByPk($memreport->zop_id);
			$product = Product::model()->findByPk($ordproduct->zp_id);
			$discount = OrdDiscount::model()->findAllByAttributes(array('zop_id'=>$memreport->zop_id));
			
			$this->render('complainthree', array(
					'model'=>$memreport,
					'productinfo'=>$product,
					'product'=>$product,
					'ordproduct'=>$ordproduct,
					'discount'=>$discount
				));
		}
		
	}

	/**
	 * 查看投诉详情
	 */
	public function actionViewComplain()
	{
		if(isset($_GET['zmrid'])){
			$model = MemReport::model()->findByPk($_GET['zmrid'],'zmr_type=1');
			$ordproduct = OrdProduct::model()->findByPk($model->zop_id);
			$product = Product::model()->findByPk($ordproduct->zp_id);
			$discount = OrdDiscount::model()->findAllByAttributes(array('zop_id'=>$model->zop_id));
			//总共俩种状态，一种是在审核，一种是处理完
			if($model->zmr_status==1 || $model->zmr_status==2){
				//正在处理
				$this->render('complaintwo',array(
						'model'=>$model,
						'product'=>$product,
						'ordproduct'=>$ordproduct,
						'discount'=>$discount
						));
			}else if($model->zmr_status==3){
				//已经处理页面
				$this->render('complainthree',array(
						'model'=>$model,
						'product'=>$product,
						'ordproduct'=>$ordproduct,
						'discount'=>$discount
						));
			}
		}else{
			$this->render('error',array( 
		            'message'=>'参数出错！',
					));
				exit;
			
		}
	}
	
	/**
	 * 举报管理
	 */
	public function actionReport()
	{
		$model = MemReport::model();
		$this->render('report',array('model'=>$model));
	}

    /*
	 *举报第一步,渲染页面
	 */
	 public function actionReportOne(){	 
	 	if(isset($_GET['productid']) && (int)$_GET['productid']){
			//获取传进来的产品id
			$this->render('report_one',array());
	 	}else{
	 		$this->render('error',array( 
		            'message'=>'参数出错！',
					));
				exit;
        }
	 }	 
	 /*
	 *举报第二步,获取提交过来的举报类型
	 */
	 public function actionReportTwo(){
	 	$model =  new MemReport();
		$productid = Yii::app()->request->getParam('productid',0);
		//限制会员一个产品一种类型只能举报一次
		$oldreport = MemReport::model()->findByAttributes(
				array('zp_id'=>$productid,
					  'zmr_type'=>'2',
					  'zmr_type_info'=>$_GET['type'],
					  'zm_id_from'=>Yii::app()->user->userid));
		/*这里报错 */
		if(!empty($oldreport->zmr_id)>0){
			$this->render('error',array( 
		            'message'=>'该产品已经举报过，请查看记录',
					));
				exit;
			
		}
		
		$criteria = new CDbCriteria();
		$criteria->select = "zp_id,zm_id,zp_name";
		$criteria->compare('zp_id', $productid);
		$productinfo = Product::model()->find($criteria);
		
		$criteria = new CDbCriteria();
		$criteria->select = "zm_id,zc_name,zc_sub_domain";
		$criteria->compare('zm_id', $productinfo->zm_id);
		$companyinfo = Company::model()->find($criteria);
		
		//print_r($companyinfo->attributes);exit;
		$this->render('report_two',array(
			'productinfo'=>$productinfo,
			'companyinfo'=>$companyinfo,
			'model'=>$model
		));
	 }
    /*
     * 举报第三步,获取提交上来的举报填写内容，保存
     * 
     */
	 public function actionReportThree(){
 	 	$model = new MemReport('reportthree');
	 	$model->attributes  =$_POST['MemReport'];
	 	$model->zmr_id      = substr(time(),-8).rand(111,999);
	 	$model->zmr_type    =2;
	 	$model->inputtime   =time();
	 	//图片
	 	$images = array_filter($_POST['images']);
	 	$model->zmr_mem_pic = array_slice($images, 0, 3);
	 	$model->save();
	 	$model->zmr_mem_pic = unserialize($model->zmr_mem_pic);
	 	$this->render('report_three',array(
	 			'model'=>$model,
			    'reporttypeval'=>$this->reporttype,
	 	));
	 } 

	/**
	 * 查看举报详情
	 */
	public function actionViewReport()
	{
		if(isset($_GET['zmrid'])){
			$model = MemReport::model()->findByPk($_GET['zmrid'],'zmr_type=2');
			//总共俩种状态，一种是在审核，一种是处理完
			if($model->zmr_status==1 || $model->zmr_status==2){
				//正在处理
				$this->render('report_three',array(
						'model'=>$model,
				));
			}else if($model->zmr_status==3){
				//已经处理页面
				$this->render('report_four',array(
						'model'=>$model,
				));
			}
		}else{
			$this->render('error',array( 
		            'message'=>'参数出错！',
					));
				exit;			
		}
	}

	/**
	 * 根据ID返回用户实例
	 */
	public function loadModel($id)
	{
		$model=Member::model()->findByPk($id);
		if($model===null)
			throw new CHttpException(404,'The requested page does not exist.');
		return $model;
	}

	/**
	 * 执行AJAX异步验证.
	 * @param CModel 要验证的模型
	 */
	protected function performAjaxValidation($model)
	{
		if(isset($_POST['ajax']) && $_POST['ajax']==='member-form')
		{
			echo CActiveForm::validate($model);
			Yii::app()->end();
		}
	}
}
